import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/odoo/purchase_supplier_evaluation/controller/add_template_tester_map_controller.dart';
import 'package:get/get.dart';

class AddTemplateTesterMapDialog extends GetView<AddTemplateTesterController> {
  AddTemplateTesterMapDialog({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var result = Scaffold(
      appBar: AppBar(
        title: Text('添加模板考评人关联'),
      ),
      body: SingleChildScrollView(
        child: Obx(() => Column(
              children: [
                Form(
                  child: Wrap(
                    children: <Widget>[
                      CrySelect(
                        width: 300,
                        label: '供应商评价模板',
                        dataList: controller.templateIdList.value,
                        value: '${controller.entity.value.supplierId}',
                        onChange: (value) async {
                          controller.entity.value.supplierId = int.parse(value);
                          // 加载部门的数据
                          // _loadDeptUsers(value);
                        },
                        onSaved: (v) async {
                          controller.entity.value.supplierId = int.parse(v);
                        },
                        validator: (v) {
                          return v!.isEmpty ? S.of(context).required : null;
                        },
                      ),
                      CrySelect(
                        width: 300,
                        label: '评价部门',
                        dataList: controller.deptIdList.value,
                        value: '${controller.entity.value.deptId}',
                        onChange: (value) async {
                          controller.entity.value.deptId = int.parse(value);
                          // 加载部门的数据
                          // _loadDeptUsers(value);
                          controller.loadDeptUsers(value);
                        },
                        onSaved: (v) async {
                          controller.entity.value.deptId = int.parse(v);
                        },
                        validator: (v) {
                          return v!.isEmpty ? S.of(context).required : null;
                        },
                      ),
                      // CryInput(
                      //   width: 280,
                      //   label: '备注',
                      //   value: purchaseEvaluationProject.comment,
                      //   onSaved: (v) {
                      //     purchaseEvaluationProject.comment = v;
                      //   },
                      // ),
                      CrySelect(
                        width: 300,
                        label: '考评人员',
                        dataList: controller.deptUserList.value,
                        value: '${controller.entity.value.testerId}',
                        onChange: (value) {
                          controller.entity.value.testerId = int.parse(value);
                        },
                        onSaved: (v) async {
                          controller.entity.value.testerId = int.parse(v);
                        },
                        validator: (v) {
                          return v!.isEmpty ? S.of(context).required : null;
                        },
                      ),
                    ],
                  ),
                )
              ],
            )),
      ),
      bottomNavigationBar: getButtonBar(context),
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
  }

  getButtonBar(BuildContext context) {
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            // FormState form = formKey.currentState!;
            // if (!form.validate()) {
            //   return;
            // }
            // form.save();
            // PurchaseEvaluationProjectOdooApi.saveOrUpdate(
            //         purchaseEvaluationProject.toOdooMap())
            //     .then((ResponseBodyApi res) {
            //   if (!res.success!) {
            //     //CryUtils.message(re s.message!);
            //     return;
            //   }
            //   Navigator.pop(context, true);
            //   // CryUtils.message(S.of(context).saved);
            // });
            // });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    return buttonBar;
  }

  // ///
  // /// 获取所有的模板数据
  // ///@Author: chendeji
  // ///@Date: 2022-05-26 08:59:21
  // ///
  // Future<void> _loadTemplate() async {
  //   ResponseBodyApi responseBodyApi =
  //       await PurchaseEvaluationTemplateOdooApi.page(data: {
  //     "page": controller.templatePage,
  //     "domainList": controller.purchaseEvaluationTemplate.toDomainList()
  //   });
  //   if (responseBodyApi.success!) {
  //     templateIdList = (responseBodyApi.data as PageModel)
  //         .records
  //         // .map((e) {
  //         //   var template = PurchaseEvaluationTemplate.fromMap(e);
  //         //   return template;
  //         // })
  //         .map((e) => SelectOptionVO(
  //             value: '${e['id']}', label: '${e['template_name']}'))
  //         .toList();
  //     entity.supplierId = int.parse(templateIdList[0].value as String);
  //   }
  // }

  // ///
  // /// 获取所有的部门
  // ///@Author: chendeji
  // ///@Date: 2022-05-26 08:59:37
  // ///
  // Future<void> _loadDeptData() async {
  //   deptIdList = await DynamicDataApi.getModelSelect(
  //     model: "hr.department",
  //     name: "complete_name",
  //     key: "parent_id",
  //     operator: "!=",
  //     val: null,
  //   );
  //   entity.deptId = int.parse('${deptIdList[0].value}');
  // }

  // ///
  // ///根据部门id获取部门的所有员工
  // ///@Author: chendeji
  // ///@Date: 2022-05-26 09:14:26
  // ///
  // Future<void> _loadDeptUsers(value) async {
  //   /**
  //              *
  //              * 选定部门后，需要刷新部门内的用户列表
  //              * @Author: chendeji
  //              * @Date: 2022-05-23 18:32:56
  //             */
  //   deptUserList = await DynamicDataApi.getUserListByDeptID(data: {
  //     'domainList': [
  //       ['dept', '=', int.parse(value)],
  //       ['template', '=', entity.supplierId]
  //     ]
  //   });
  //   setState(() {});
  // }
}
